package cn.sxt.yxl.common.util;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * 蜘蛛的工具类(爬虫)
 * @author wangshSxt
 *
 */
public class YxlSpiderUtil
{
	/**
	 * 抓取测试项的列表页面
	 * 相当于栏目页面
	 * @param column 栏目
	 * @result 
	 * {"code":"0","info":"","data":{"totalPage":"28","itemsList":[{"name":"","url":"","imgPath":"","testedNum":"","favNum":"","pubTime":"","suggNum":"","questionNum":""}]}}
	 */
	public JSONObject itemsList(String column,int page)
	{
		ConstatFinalUtil.LOGGER.info("---- itemsList ----");
		JSONObject resultJSON = new JSONObject() ;
		/* 抓取的Url */
		String urlStr = "http://www.xinli001.com/ceshi/" + column + "?page=" + page;
		/* 请求服务器 */
		/*HttpUtil httpUtil = new HttpUtil();
		Map<String, String> paramsMap = new HashMap<String,String>();
		httpUtil.methodGet(urlStr, Collections.EMPTY_MAP, paramsMap);*/
		
		/* 采用Jsoup */
		try
		{
			/* 请求服务器 */
			Connection connection = Jsoup.connect(urlStr);
			/* 连接,获取Document对象
			 * jsop Document===JS里面的 Document*/
			Document document = connection.get() ;
			
			/* 获取数据 */
			/*ConstatFinalUtil.LOGGER.info("document:{}",document);*/
			Elements listShowEles = document.getElementsByClass("list_show");
			/* 取第一个元素 */
			Element listShowEle = listShowEles.get(0);
			/* list_show下面的fixed */
			Elements fixedEles = listShowEle.getElementsByClass("fixed");
			ConstatFinalUtil.LOGGER.info("listShowEles大小:{};fixedEles大小:{}",listShowEles.size(),fixedEles.size());
			
			/* 数据 */
			JSONArray itemsArr = new JSONArray();
			for (Iterator iterator = fixedEles.iterator(); iterator.hasNext();)
			{
				Element fiexEle = (Element) iterator.next();
				JSONObject itemsJSON = new JSONObject() ; 
				
				/* 属性值必须填写完整 */
				/*System.out.println("----" + fiexEle.getElementsByAttributeValue("class", "linfo fl"));*/
				/* 测试项的数据 */
				/* 取名称,class=linfo的内容 */
				Element linfoEle = fiexEle.getElementsByClass("linfo").get(0);
				
				/* 取到A链接 */
				Element aEle = linfoEle.getElementsByTag("a").get(0);
				/* 取到开始标签和结束标签中间的内容 */
				String name = aEle.text();
				String href = aEle.attr("href");
				String id = href.substring(href.lastIndexOf("/") + 1,href.length());
				
				/* 取图片的地址 */
				Element pboxEle = fiexEle.getElementsByClass("pbox").get(0);
				Element imgEle = pboxEle.getElementsByTag("img").get(0);
				String imgPath = imgEle.attr("src");
				
				/* 调用测试项详情的页面,获取有用的信息:包含人数,内容 */
				JSONObject itemInfoResJSON = this.itemInfo(href);
				if("0".equalsIgnoreCase(itemInfoResJSON.getString("code")))
				{
					/* 获取值 */
					String testedNum = itemInfoResJSON.getString("testedNum") ;
					/* 查询数据 */
					itemsJSON = itemInfoResJSON.getJSONObject("data");
				}
				/*ConstatFinalUtil.LOGGER.info("name:{},href:{};imtPath:{}",name,href,imtPath);*/
				
				/* 存储数据 */
				itemsJSON.put("name", name);
				itemsJSON.put("imgPath", imgPath);
				itemsJSON.put("id", id);
				itemsArr.add(itemsJSON);
			}
			
			JSONObject dataJSON = new JSONObject() ; 
			dataJSON.put("itemsList", itemsArr);
			
			/* 总页数 */
			Elements pageAEles = document.getElementById("pages").getElementsByTag("a");
			/* 取到最后一个元素 */
			Element totalPageEle = pageAEles.get(pageAEles.size() - 2);
			dataJSON.put("totalPage", totalPageEle.text());
			/* 存储json返回的数据 */
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		} catch (IOException e)
		{
			ConstatFinalUtil.LOGGER.error("请求yxl测试项列表报错了;url:{}",
					urlStr,e);
			resultJSON.put("code", "1");
			resultJSON.put("info", "报错了;" + e);
		} 
		return resultJSON ; 
	}
	
	/**
	 * 取测试项的详情
	 * @result 
	 * {"code":"0","info":"","data":{"testedNum":"","favNum":"","pubTime":"","suggNum":"","questionNum":""}}
	 */
	public JSONObject itemInfo(String urlStr)
	{
		ConstatFinalUtil.LOGGER.info("---- itemInfo ----");
		JSONObject resultJSON = new JSONObject() ;
		try
		{
			/* 请求服务器 */
			Connection connection = Jsoup.connect(urlStr);
			/* 连接,获取Document对象
			 * jsop Document===JS里面的 Document*/
			Document document = connection.get() ;
			
			/* 解析测试详情里面的内容 */
		} catch (IOException e)
		{
			ConstatFinalUtil.LOGGER.error("请求yxl测试项详情报错了;url:{}",
					urlStr,e);
		}
		
		return resultJSON ; 
	}
}
